﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Collections;
using NHibernate.Mapping.ByCode;

namespace SGC.DL.Comun
{
    internal static class Generador_PK
    {        
        internal static string Generador_ID(string Prefijo, int Longitud, string Tabla, string CampoID)
        {
            string id = string.Empty;
            string sqlConsulta = string.Empty;
            int nroExtraer = Longitud - Prefijo.Length;
            sqlConsulta = "SELECT ISNULL(MAX(CONVERT(INT,RIGHT(" + CampoID + "," + nroExtraer.ToString() +
                            "))),0) + 1 AS ID FROM " + Tabla ;
            ISession session = DL_NHibernate.GetSession().OpenSession();            
            IList result = session.CreateSQLQuery(sqlConsulta).List() ;
            id = Prefijo + result[0].ToString().PadLeft(nroExtraer, '0');   
            return id;
        }
    }
}
